﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Contacto
{
    public partial class crear : Form
    {
        public crear()
        {
            InitializeComponent();
        }

        private void btn_Guardar_Click(object sender, EventArgs e)
        {
            if (tx_name.Text != " " && tx_name.Text != "")
            {
                string name, lastname, day, mes, year, phone, mail, address;
                name = tx_name.Text; lastname = tx_lastname.Text; day = tx_day.Text; mes = tx_mes.Text; year = tx_year.Text; phone = tx_phone.Text; mail = tx_mail.Text; address = tx_address.Text;
                string ruta = "Contactos\\" + name;
                if ((System.IO.Directory.Exists(ruta)) == false)
                {
                    System.IO.Directory.CreateDirectory("Contactos\\" + name);
                    string rt_name = @"Contactos\\" + name + "\\name", rt_lastname = @"Contactos\\" + name + "\\lastname", rt_day = @"Contactos\\" + name + "\\day", rt_mes = @"Contactos\\" + name + "\\mes", rt_year = @"Contactos\\" + name + "\\year", rt_phone = @"Contactos\\" + name + "\\phone", rt_mail = @"Contactos\\" + name + "\\mail", rt_address = @"Contactos\\" + name + "\\address";
                    System.IO.StreamWriter f_name = new System.IO.StreamWriter(rt_name);
                    f_name.WriteLine(name);
                    f_name.Close();
                    System.IO.StreamWriter f_last = new System.IO.StreamWriter(rt_lastname);
                    f_last.WriteLine(lastname);
                    f_last.Close();
                    System.IO.StreamWriter f_day = new System.IO.StreamWriter(rt_day);
                    f_day.WriteLine(day);
                    f_day.Close();
                    System.IO.StreamWriter f_mes = new System.IO.StreamWriter(rt_mes);
                    f_mes.WriteLine(mes);
                    f_mes.Close();
                    System.IO.StreamWriter f_year = new System.IO.StreamWriter(rt_year);
                    f_year.WriteLine(year);
                    f_year.Close();
                    System.IO.StreamWriter f_phone = new System.IO.StreamWriter(rt_phone);
                    f_phone.WriteLine(phone);
                    f_phone.Close();
                    System.IO.StreamWriter f_mail = new System.IO.StreamWriter(rt_mail);
                    f_mail.WriteLine(mail);
                    f_mail.Close();
                    System.IO.StreamWriter f_address = new System.IO.StreamWriter(rt_address);
                    f_address.WriteLine(address);
                    f_address.Close();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Ese contacto ya existe", "Aviso de error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void tx_day_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ' ')
            {
                e.KeyChar = '\0';
            }
            if (Char.IsDigit(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void brn_Cancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
